<?
// Date in the past 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1 
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0 
header("Pragma: no-cache");

include_once('mq.php');
if (session_id() == '')
    session_start();

if (isset($_GET['logout']))
    MQ_Logout();

if (!empty($_SESSION['mq_l'])) {
    $time = mktime(23, 59, 59, date('n'), date('j'), date('Y'));
    $login = substr((int) $_SESSION['mq_l'], 0, 14);
    $password = substr($_SESSION['mq_p'], 0, 16);

    $res = MQ_Query('USERHISTORY-login=' . $login . '|password=' . $password . '|from=' . ($time - 2592000) . '|to=' . $time
            , T_CACHEDIR
            , T_CACHETIME
            , $login . '/');
    if ($res == '!!!CAN\'T CONNECT!!!') {
        $this->_CONNECTION_TIMEOUT = true;
        return;
    }
}

if (empty($res) || strpos($res, 'Invalid') !== false || strpos($res, 'Disabled') !== false) {
    MQ_Logout();
    exit;
}
?>
<html>
    <head>
        <title>Account information::Account history</title>
    </head>
    <body>
        <table cellspacing="0" cellpadding="0" border="0" width="100%" style="background-color: buttonface;">
            <tr bgcolor="#ffffff"><td align="right" style="font-size:12px; font-family:Tahoma, Arial, Helvetica, sans-serif;">
                    <?php
                    $info = explode("\r\n", $res);
                    $size = sizeof($info);

                    $account = $info[0];
                    $name = $info[1];
                    ?>
            <nobr><b>Account:</b> <?= $account ?>
                &nbsp;|&nbsp; <b>Name:</b> <?= $name ?>
                &nbsp;|&nbsp; <a href="?logout=1" onCLick="return confirm('Are you sure?');">Logout</a></nobr><br>
            <img src="img/0.gif" width="1" height="5"/>
        </td></tr>
    <tr><td>
            <table cellspacing="1" cellpadding="3" border="0" width="100%" style="font-size:12px; font-family:Tahoma, Arial, Helvetica, sans-serif;">
                <tr align="right" style="background-color: #a0a0a0;">
                    <td align="left"><b>Order</b></td>
                    <td><b>Time</b></td>
                    <td><b>Type</b></td>
                    <td><b>Lots</b></td>
                    <td><b>Symbol</b></td>
                    <td><b>Price</b></td>
                    <td><b>S/L</b></td>
                    <td><b>T/P</b></td>
                    <td><b>Time</b></td>
                    <td><b>Price</b></td>
                    <td><b>Swap</b></td>
                    <td><b>Profit</b></td>
                </tr>
                <?php
                $beginIndex = 3;

                $cnt = 0;
                for ($i = $beginIndex; $i < $size; $i++) {
                    if ($info[$i] === '0')
                        break;
                    $row = explode("\t", $info[$i]);
                    if (strpos($row[2], 'balance') !== false) {
                        ?>
                        <tr align="right" bgcolor="<?= ($cnt % 2 ? '#e0e0e0' : '#ffffff') ?>">
                            <td align="left"><?= $row[0] ?></td>
                            <td><?= $row[1] ?></td>
                            <td><?= $row[2] ?></td>
                            <td align="left" colspan="8"><nobr><?= $row[13] ?><nobr></td>
                                <td><nobr><?= $row[12] ?><nobr></td>
                                        </tr>
        <?php
    } else {
        ?>
                                        <tr align="right" bgcolor="<?= ($cnt % 2 ? '#e0e0e0' : '#ffffff') ?>">
                                            <td align="left"><?= $row[0] ?></td>
                                            <td><?= $row[1] ?></td>
                                            <td><?= $row[2] ?></td>
                                            <td><nobr><?= $row[3] ?></nobr></td>
                                        <td><?= strtolower($row[4]) ?></td>
                                        <td><nobr><?= $row[5] ?><nobr></td>
                                                <td><nobr><?= $row[6] ?><nobr></td>
                                                        <td><nobr><?= $row[7] ?><nobr></td>
                                                                <td><nobr><?= $row[8] ?><nobr></td>
                                                                        <td><nobr><?= $row[9] ?><nobr></td>
        <?php
        if (strpos($row[2], 'limit') !== false) {
            ?>
                                                                                    <td colspan="2" align="right"><?= $row[13] ?></td>
                                                                                    <?php
                                                                                } else {
                                                                                    ?>
                                                                                    <td><nobr><?= $row[11] ?><nobr></td>
                                                                                            <td><nobr><?= $row[12] ?><nobr></td>
                                                                                    <?php
                                                                                }
                                                                                ?>
                                                                                                </tr>
                                                                                <?php
                                                                            }
                                                                            ++$cnt;
                                                                        }
                                                                        $profit_loss = MQ_GetParam($info[$i + 6]);
                                                                        $deposit = MQ_GetParam($info[$i + 1]);
                                                                        $credit = MQ_GetParam($info[$i + 3]);
                                                                        $withdrawal = MQ_GetParam($info[$i + 2]);
                                                                        $profit = $deposit + $profit_loss;
                                                                        ?>
                                                                                        <tr bgcolor="#cccccc">
                                                                                            <td colspan="11">
                                                                                                <b>Profit/Loss: <nobr><?= $profit_loss ?></nobr>
                                                                                                    Credit: <nobr><?= $credit ?></nobr>
                                                                                                    Deposit: <nobr><?= $deposit ?></nobr>
                                                                                                    Withdrawal: <nobr><?= $withdrawal ?></nobr></b></td>
                                                                                            <td align="right"><b><?= $profit ?></b></td>
                                                                                        </tr>
                                                                                        </table>
                                                                                        </td></tr>
                                                                                        <tr bgcolor="#ffffff"><td style="font-size:12px; font-family:Tahoma, Arial, Helvetica, sans-serif;">
                                                                                                <img src="img/0.gif" width="1" height="5"/><br>
                                                                                        <nobr><a href="trade.php">Trade</a> &nbsp;|&nbsp; <b>Account history</b></nobr>
                                                                                        </td></tr>
                                                                                        </table>
                                                                                        </body>
                                                                                        </html>